{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# extract data from heatmaps\n",
    "import cv2 as cv\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "img = cv.imread(\"images/heatmap.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(288, 432, 3)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f6d3c1e2e80>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAADsAAAD8CAYAAADJyNy/AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAACpVJREFUeJztnX2MHVUZxn+PQAkCho9dSKPUbU0lotGCG8QQEEVkIcaq\niQgaRCVWEkgg8octJGpMSBQFE2PUQCBAggUUGhtTCw0hEhJBdpGvWiotghaasisGiFSg5fGPORdv\nt/funbkzwxz3zJNM7p0zM2fOk/fMmXee8847sk0qeFvTDXgr0ZKdr2jJzle0ZKuApAlJmyVtkbSy\nrvMUgu3KF2AfYCuwBFgAPAIcU8e5iix1WfZ4YIvtp2y/BtwCLK/pXLmxb031vhP4R9f6NuAj/XYe\nGRnx2NjY0CebmpqasT06aL+6yKpH2R5+qaQVwAqARYsWMTk5OfzJpGfy7FdXN94GHNW1/i7gue4d\nbF9je9z2+OjoQKNUgros+yCwVNJi4FngbOBLeQ+emJhgZmZm4H47d+7kgAMOy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W4vKt8Ndnrbq+T9YDzgcPJInOeDL+HFTlX60HNV7Rk5ytasvMVLdn5iqTI/hfi33Sg6gE9nAAA\nAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f6d6ea5e438>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# approximate bounding box around the legend\n",
    "legend_box = [335, 351, 32, 254] # [xmin, xmax, ymin, ymax]\n",
    "\n",
    "# calibrate legend with data\n",
    "legend_data = [5, 20] # [val1, val2]\n",
    "legend_data_px = [(344,216), (344,80)] # [(x,y), (x,y)]\n",
    "legend_data_px = [(legend_data_px[0][0] - legend_box[0], legend_data_px[0][1] - legend_box[2]),\n",
    "                  (legend_data_px[1][0] - legend_box[0], legend_data_px[1][1] - legend_box[2])]\n",
    "\n",
    "# crop\n",
    "legend_img = img[legend_box[2]:legend_box[3], legend_box[0]:legend_box[1]]\n",
    "plt.imshow(cv.cvtColor(legend_img, cv.COLOR_BGR2RGB))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Legend Calibration\n",
    "\n",
    "Assume:\n",
    "    1. approx_val = f(R,G,B)\n",
    "\n",
    "At calibration points:\n",
    "    2. val1 = f(R1,G1,B1) at (x1,y1)\n",
    "    3. val2 = f(R2,G2,B2) at (x2,y2)\n",
    "\n",
    "At any (x_l,y_l) on the legend:\n",
    "    4. val_l = f(R,G,B) at (x_l,y_l)\n",
    "\n",
    "If we assume (x_l,y_l) lies on the line joining (x1,y1) and (x2,y2), then val can be calculated at any (x_l,y_l) assuming linear spacing.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7f6d3c12d518>]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5P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QfaB5R9JP936HWzAOgX\n7fg92h/Phv7e+ZQlsRYVHv+70P5YBpwa7fgjtE+Opqz0Mh+YG/o5rT68TnRkrIiIz8Vz6UZERKpA\niV5ExOeU6EVEfE6JXkTE55ToRUR8ToleRMTnlOhFRHxOiV5ExOf+P1AZmShzIVJTAAAAAElFTkSu\nQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f6d3c14f6a0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot intensity along the legend \"line\":\n",
    "ylist = [v for v in range(0,legend_img.shape[0])]\n",
    "intensity = np.sqrt([legend_img[y,12,0]**2 + legend_img[y,12,1]**2 + legend_img[y,12,2]**2 for y in ylist])\n",
    "plt.plot(ylist, intensity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  3, 219, 220])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# find edges\n",
    "intensity_jumps = [intensity[i]-intensity[i-1] > 10 for i in range(1,len(intensity))]\n",
    "intensity_jumps = np.array([j for j in range(0,len(intensity_jumps)) if intensity_jumps[j] == True])\n",
    "intensity_jumps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 218\n"
     ]
    }
   ],
   "source": [
    "# for smooth legend, it's easy to pick the pick min, max:\n",
    "# |----|---------x----------|----|----\n",
    "#      i_min    mid-pt     i_max\n",
    "\n",
    "i_max = np.min(intensity_jumps[intensity_jumps - len(intensity)/2 > 0]) - 1\n",
    "i_min = np.max(intensity_jumps[intensity_jumps - len(intensity)/2 < 0]) + 1\n",
    "\n",
    "print(i_min, i_max)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[24.852941176470587,\n",
       " 24.74264705882353,\n",
       " 24.63235294117647,\n",
       " 24.522058823529413,\n",
       " 24.41176470588235,\n",
       " 24.301470588235293,\n",
       " 24.191176470588236,\n",
       " 24.080882352941178,\n",
       " 23.970588235294116,\n",
       " 23.860294117647058,\n",
       " 23.75,\n",
       " 23.639705882352942,\n",
       " 23.529411764705884,\n",
       " 23.419117647058822,\n",
       " 23.308823529411764,\n",
       " 23.198529411764707,\n",
       " 23.08823529411765,\n",
       " 22.977941176470587,\n",
       " 22.86764705882353,\n",
       " 22.75735294117647,\n",
       " 22.647058823529413,\n",
       " 22.53676470588235,\n",
       " 22.426470588235293,\n",
       " 22.316176470588236,\n",
       " 22.205882352941178,\n",
       " 22.095588235294116,\n",
       " 21.985294117647058,\n",
       " 21.875,\n",
       " 21.764705882352942,\n",
       " 21.654411764705884,\n",
       " 21.544117647058822,\n",
       " 21.433823529411764,\n",
       " 21.323529411764707,\n",
       " 21.21323529411765,\n",
       " 21.102941176470587,\n",
       " 20.99264705882353,\n",
       " 20.88235294117647,\n",
       " 20.772058823529413,\n",
       " 20.661764705882355,\n",
       " 20.551470588235293,\n",
       " 20.441176470588236,\n",
       " 20.330882352941174,\n",
       " 20.220588235294116,\n",
       " 20.110294117647058,\n",
       " 20.0,\n",
       " 19.889705882352942,\n",
       " 19.779411764705884,\n",
       " 19.669117647058826,\n",
       " 19.558823529411764,\n",
       " 19.448529411764707,\n",
       " 19.338235294117645,\n",
       " 19.227941176470587,\n",
       " 19.11764705882353,\n",
       " 19.00735294117647,\n",
       " 18.897058823529413,\n",
       " 18.786764705882355,\n",
       " 18.676470588235293,\n",
       " 18.566176470588236,\n",
       " 18.455882352941174,\n",
       " 18.345588235294116,\n",
       " 18.235294117647058,\n",
       " 18.125,\n",
       " 18.014705882352942,\n",
       " 17.904411764705884,\n",
       " 17.794117647058826,\n",
       " 17.683823529411764,\n",
       " 17.573529411764707,\n",
       " 17.463235294117645,\n",
       " 17.352941176470587,\n",
       " 17.24264705882353,\n",
       " 17.13235294117647,\n",
       " 17.022058823529413,\n",
       " 16.911764705882355,\n",
       " 16.801470588235293,\n",
       " 16.691176470588236,\n",
       " 16.580882352941174,\n",
       " 16.470588235294116,\n",
       " 16.360294117647058,\n",
       " 16.25,\n",
       " 16.139705882352942,\n",
       " 16.029411764705884,\n",
       " 15.919117647058824,\n",
       " 15.808823529411764,\n",
       " 15.698529411764707,\n",
       " 15.588235294117647,\n",
       " 15.477941176470589,\n",
       " 15.367647058823529,\n",
       " 15.257352941176471,\n",
       " 15.147058823529411,\n",
       " 15.036764705882353,\n",
       " 14.926470588235293,\n",
       " 14.816176470588236,\n",
       " 14.705882352941176,\n",
       " 14.595588235294118,\n",
       " 14.485294117647058,\n",
       " 14.375,\n",
       " 14.264705882352942,\n",
       " 14.154411764705882,\n",
       " 14.044117647058824,\n",
       " 13.933823529411764,\n",
       " 13.823529411764707,\n",
       " 13.713235294117647,\n",
       " 13.602941176470589,\n",
       " 13.492647058823529,\n",
       " 13.382352941176471,\n",
       " 13.272058823529411,\n",
       " 13.161764705882353,\n",
       " 13.051470588235293,\n",
       " 12.941176470588236,\n",
       " 12.830882352941178,\n",
       " 12.720588235294118,\n",
       " 12.610294117647058,\n",
       " 12.5,\n",
       " 12.389705882352942,\n",
       " 12.279411764705882,\n",
       " 12.169117647058822,\n",
       " 12.058823529411764,\n",
       " 11.948529411764707,\n",
       " 11.838235294117647,\n",
       " 11.727941176470587,\n",
       " 11.617647058823529,\n",
       " 11.507352941176471,\n",
       " 11.397058823529413,\n",
       " 11.286764705882353,\n",
       " 11.176470588235293,\n",
       " 11.066176470588236,\n",
       " 10.955882352941178,\n",
       " 10.845588235294118,\n",
       " 10.735294117647058,\n",
       " 10.625,\n",
       " 10.514705882352942,\n",
       " 10.404411764705882,\n",
       " 10.294117647058822,\n",
       " 10.183823529411764,\n",
       " 10.073529411764707,\n",
       " 9.963235294117647,\n",
       " 9.852941176470587,\n",
       " 9.742647058823529,\n",
       " 9.632352941176471,\n",
       " 9.522058823529413,\n",
       " 9.411764705882353,\n",
       " 9.301470588235293,\n",
       " 9.191176470588236,\n",
       " 9.080882352941178,\n",
       " 8.970588235294118,\n",
       " 8.860294117647058,\n",
       " 8.75,\n",
       " 8.639705882352942,\n",
       " 8.529411764705882,\n",
       " 8.419117647058822,\n",
       " 8.308823529411764,\n",
       " 8.198529411764707,\n",
       " 8.088235294117647,\n",
       " 7.977941176470589,\n",
       " 7.867647058823529,\n",
       " 7.757352941176471,\n",
       " 7.647058823529411,\n",
       " 7.536764705882353,\n",
       " 7.4264705882352935,\n",
       " 7.3161764705882355,\n",
       " 7.205882352941177,\n",
       " 7.095588235294118,\n",
       " 6.985294117647059,\n",
       " 6.875,\n",
       " 6.764705882352941,\n",
       " 6.654411764705882,\n",
       " 6.544117647058823,\n",
       " 6.4338235294117645,\n",
       " 6.323529411764706,\n",
       " 6.213235294117647,\n",
       " 6.102941176470589,\n",
       " 5.992647058823529,\n",
       " 5.882352941176471,\n",
       " 5.772058823529412,\n",
       " 5.661764705882353,\n",
       " 5.551470588235294,\n",
       " 5.4411764705882355,\n",
       " 5.330882352941177,\n",
       " 5.220588235294118,\n",
       " 5.110294117647059,\n",
       " 5.0,\n",
       " 4.889705882352941,\n",
       " 4.779411764705882,\n",
       " 4.669117647058823,\n",
       " 4.5588235294117645,\n",
       " 4.448529411764706,\n",
       " 4.338235294117647,\n",
       " 4.227941176470588,\n",
       " 4.117647058823529,\n",
       " 4.007352941176471,\n",
       " 3.8970588235294117,\n",
       " 3.7867647058823533,\n",
       " 3.6764705882352944,\n",
       " 3.5661764705882355,\n",
       " 3.4558823529411766,\n",
       " 3.3455882352941178,\n",
       " 3.235294117647059,\n",
       " 3.125,\n",
       " 3.014705882352941,\n",
       " 2.9044117647058822,\n",
       " 2.7941176470588234,\n",
       " 2.6838235294117645,\n",
       " 2.573529411764706,\n",
       " 2.463235294117647,\n",
       " 2.3529411764705883,\n",
       " 2.2426470588235294,\n",
       " 2.1323529411764706,\n",
       " 2.0220588235294117,\n",
       " 1.9117647058823528,\n",
       " 1.801470588235294,\n",
       " 1.6911764705882355,\n",
       " 1.5808823529411766,\n",
       " 1.4705882352941178,\n",
       " 1.3602941176470589]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# build a list of (r,g,b)->val\n",
    "rgb_list = [(legend_img[y,12,0], legend_img[y,12,1], legend_img[y,12,2]) for y in range(i_min,i_max)]\n",
    "val_list = [(legend_data[1] - legend_data[0])*(y - legend_data_px[0][1])/(legend_data_px[1][1] - legend_data_px[0][1]) + legend_data[0] for y in range(i_min,i_max)]\n",
    "val_list    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/lib/python3.6/site-packages/ipykernel_launcher.py:4: RuntimeWarning: overflow encountered in ubyte_scalars\n",
      "  after removing the cwd from sys.path.\n"
     ]
    }
   ],
   "source": [
    "# do extraction by finding nearest value and calculate error\n",
    "\n",
    "def valForRGB(r,g,b):\n",
    "    return val_list[np.argmin(np.array([(c[0]-r)**2 + (c[1]-g)**2 + (c[2]-b)**2 for c in rgb_list]))]\n",
    "\n",
    "results = []\n",
    "for xi in range(125,300,43):\n",
    "    for yi in range(56,232,43):\n",
    "        (r,g,b) = (img[yi,xi,0], img[yi,xi,1], img[yi,xi,2])\n",
    "        results.append((xi,yi,valForRGB(r,g,b)))\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(125, 56, 1.3602941176470589),\n",
       " (125, 99, 6.102941176470589),\n",
       " (125, 142, 11.066176470588236),\n",
       " (125, 185, 16.029411764705884),\n",
       " (125, 228, 20.99264705882353),\n",
       " (168, 56, 2.0220588235294117),\n",
       " (168, 99, 7.095588235294118),\n",
       " (168, 142, 12.058823529411764),\n",
       " (168, 185, 17.022058823529413),\n",
       " (168, 228, 22.095588235294116),\n",
       " (211, 56, 3.014705882352941),\n",
       " (211, 99, 7.977941176470589),\n",
       " (211, 142, 13.051470588235293),\n",
       " (211, 185, 18.014705882352942),\n",
       " (211, 228, 22.977941176470587),\n",
       " (254, 56, 4.117647058823529),\n",
       " (254, 99, 9.080882352941178),\n",
       " (254, 142, 14.044117647058824),\n",
       " (254, 185, 19.11764705882353),\n",
       " (254, 228, 24.080882352941178),\n",
       " (297, 56, 5.0),\n",
       " (297, 99, 10.073529411764707),\n",
       " (297, 142, 15.036764705882353),\n",
       " (297, 185, 20.0),\n",
       " (297, 228, 9.742647058823529)]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
